package com.soft.dao;

import com.soft.entity.Order;
import com.soft.util.Connect;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.SimpleFormatter;

public class OrderDao {

    Class<Order> orderClass;

    public OrderDao() {
        orderClass = Order.class;
    }

    public List<Order> getAll() {
        List<Object> param = new ArrayList<Object>();
        String sql = "select * from `order`";
        List<Order> list = Connect.getInstance().queryListObject(sql, param, orderClass);
        return list;
    }

    public Order get(int id) {
        List<Object> param = new ArrayList<>();
        param.add(id);
        String sql = "select * from `order` where id=?";
        return Connect.getInstance().queryObject(sql, param, orderClass);
    }

    public List<Order> getByBuyer(String buyer) {
        List<Object> param = new ArrayList<>();
        param.add(buyer);

        String sql = "select * from `order` where buyer=?";
        return Connect.getInstance().queryListObject(sql, param, orderClass);
    }

    public boolean update(Order order) {
        List<Object> param = new ArrayList<Object>();
        param.add(order.getBuyer());
        param.add(order.getReceiver());
        param.add(order.getTypeNumber());
        param.add(order.getPayMethod());
        param.add(order.getDeliveryMethod());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        param.add(sdf.format(order.getTime()));
        param.add(order.getStatus());
        param.add(order.getDetail());
        param.add(order.getCloseDetail());
        param.add(order.getId());

        String sql = "update `order` set buyer=?, receiver=?, typenumber=?, paymethod=?, deliverymethod=?," +
                "time=?, status=?, detail=?, closeDetail=? where id = ?" ;

        int flag = Connect.getInstance().execute(sql, param);
        if (flag != 0) {
            return true;
        }
        return  false;
    }

    /**
     * @author kongz 2021/6/7 16:54
     * @description 新建一个订单
     * @param order 订单实体类
     * @return boolean
     */
    public boolean insert(Order order) {
        List<Object> param = new ArrayList<>();
        param.add(order.getBuyer());
        param.add(order.getReceiver());
        param.add(order.getTypeNumber());
        param.add(order.getPayMethod());
        param.add(order.getDeliveryMethod());
        param.add(order.getTime());
        param.add(order.getCloseDetail());
        param.add(order.getDetail());

        String sql = "insert into `order`(buyer, receiver, typenumber, paymethod, " +
                "deliverymethod, time, closeDetail, detail) value (?,?,?,?,?,?,?,?)";

        if (Connect.getInstance().execute(sql, param) != 0)
            return true;
        return false;
    }
}
